// 分享
var express = require('express');
var router = express.Router();
var db = require('../model/index');

/* GET 美景 */
router.get('/shareCamera', function (req, res) {
    var sql = `SELECT * FROM shareList where shareType = 'camera'`
    db(sql, null, function (err, data) {
        if (!err) {
            //    console.log(123);
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 美食
router.get('/shareFood', function (req, res) {
    var sql = `SELECT * FROM shareList where shareType = 'food'`
    db(sql, null, function (err, data) {
        if (!err) {
            if (data.length > 0) {
                res.send({
                    code: 200,
                    message: "success",
                    data: data,
                })

            } else {
                res.send({
                    code: 500,
                    message: "fail"
                })
            }

        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 寻物留言
router.get('/updateTake', function (req, res) {
    //   res.send('respond with a resource');
    if (req.query.leaveMessageAudit == 1) {
        var sql = `UPDATE takemessage SET leaveMessage=? WHERE takeId = ? and userId = ?`
    } else {
        var sql = `INSERT into takemessage (leaveMessage,takeId,userId) VALUES (?,?,?)`
    }
    db(sql, [req.query.leaveMessage, req.query.takeId, req.query.userId], function (err, data) {
        //    db(sql,null,function(err,data){

        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    })

});
// 寻物举报
router.get('/jubaoTake', function (req, res) {
    var sql = `INSERT into takejubao (jubao,takeId,userId) VALUES (?,?,?);UPDATE takelist SET takeAudit=2 WHERE takeId = ?`
    db(sql, [req.query.jubao, req.query.takeId, req.query.userId, req.query.takeId], function (err, data) {
        if (!err) {
            //    console.log(123);
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});
// 添加
router.post('/addShare', function (req, res) {
    console.log(req.body);
    var sql = `INSERT into sharelist (shareImg,shareDesc,
                shareTitle,
                shareType,InfoPublisher) VALUES (?)
            `

    let params = [req.body.shareImg, req.body.shareDesc,
    req.body.shareTitle,
    req.body.shareType, req.body.InfoPublisher]

    db(sql, [params], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail"
            })
        }
    });

});
// 寻物图片批量
//            router.post('/addTakeImgs', function(req, res) {
//             let arr = req.body.takeImgs; // 前端提交过来的数据
// console.log(arr,11);
// let sql = `INSERT INTO takeimg (takeId,takeImg) VALUES(?,?)`    // 插入语句
// for(let i=0;i<arr.length;i++){
//     db(sql,[req.body.takeId,arr[i]],function(err,data){
//         if(err){
//             console.log(err)
//             if(i==arr.length-1){
//                 res.send({
//                     code:500,  
//                     msg:"添加失败!",
//                 });
//             }
//             }else {
//                 console.log(data);
//             if(i==arr.length-1){
//                 res.send({
//                     msg:"添加成功!",
//                      code:200
//             });
//             }
//         }
// });
// }    
//         });
// 美景搜索
router.get('/searchCamera', function (req, res) {
    console.log(req.query.searchTitle);
    var sql = `select * from sharelist where shareType = 'camera' and shareTitle  REGEXP ?`
    db(sql, [req.query.searchTitle], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail",
            })
        }
    });

});
// 美食搜索
router.get('/searchFood', function (req, res) {
    console.log(req.query.searchTitle);
    var sql = `select * from sharelist where shareType = 'food' and shareTitle  REGEXP ?`
    db(sql, [req.query.searchTitle], function (err, data) {
        if (!err) {
            res.send({
                code: 200,
                message: "success",
                data: data,
            })
        } else {
            res.send({
                code: 501,
                message: "dbfail",
            })
        }
    });

});
module.exports = router;